<?php
class ControllerSaleCustomerBlacklist extends AdminController {

  	public function index() {
		M('sale/customer_blacklist');
    	$this->getList();
  	}

  	public function insert() {

		M('sale/customer_blacklist');

		if ($this->request->isPost() && $this->validateForm()) {
      	  	$this->model_sale_customer_blacklist->addCustomerBlacklist($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/customer_blacklist'));
		}

    	$this->getForm();
  	}

  	/**
  	 * add ban ip by ajax
  	 */
  	public function add() {
  		M('sale/customer_blacklist');
  		$ip = G('ip');
  		$rs = $this->db->runSql("INSERT INTO @@customer_ip_blacklist(ip) VALUES('$ip')");
  		$this->response->setOutput(json_encode(array('status' => $rs)));
  	}
  	
  	
  	public function update() {

		M('sale/customer_blacklist');

    	if ($this->request->isPost() && $this->validateForm()) {
			$this->model_sale_customer_blacklist->editCustomerBlacklist($this->request->get['customer_ip_blacklist_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/customer_blacklist'));
		}

    	$this->getForm();
  	}

  	public function delete() {

		M('sale/customer_blacklist');

    	if (isset($this->request->post['selected'])) {
			foreach ($this->request->post['selected'] as $customer_ip_blacklist_id) {
				$this->model_sale_customer_blacklist->deleteCustomerBlacklist($customer_ip_blacklist_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/customer_blacklist'));
    	}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$this->data['customer_blacklists'] = array();

		$customer_blacklist_total = $this->model_sale_customer_blacklist->getTotalCustomerBlacklists();

		$results = $this->model_sale_customer_blacklist->getCustomerBlacklists($qf);

    	foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->_['text_edit'],
				'href' => UA('sale/customer_blacklist/update', 'customer_ip_blacklist_id=' . $result['customer_ip_blacklist_id'])
			);

			$this->data['customer_blacklists'][] = array(
				'customer_ip_blacklist_id' => $result['customer_ip_blacklist_id'],
				'ip'                       => $result['ip'],
				'total'                    => $result['total'],
				'customer'                 => UA('sale/customer', 'filter_ip=' . $result['ip']),
				'action'                   => $action
			);
		}

		$this->data['pagination'] = $this->pagination($customer_blacklist_total)->render();

		$this->display('sale/customer_blacklist_list.tpl');
  	}

  	protected function getForm() {
    	$this->document->setTitle($this->_['heading_title']);

		if (!isset($this->request->get['customer_ip_blacklist_id'])) {
			$this->data['action'] = UA('sale/customer_blacklist/insert');
		} else {
			$this->data['action'] = UA('sale/customer_blacklist/update', 'customer_ip_blacklist_id=' . $this->request->get['customer_ip_blacklist_id']);
		}

    	$this->data['cancel'] = UA('sale/customer_blacklist');

    	if (isset($this->request->get['customer_ip_blacklist_id']) && !$this->request->isPost()) {
      		$customer_blacklist_info = $this->model_sale_customer_blacklist->getCustomerBlacklist($this->request->get['customer_ip_blacklist_id']);
    	}

    	if (isset($this->request->post['ip'])) {
      		$this->data['ip'] = $this->request->post['ip'];
		} elseif (!empty($customer_blacklist_info)) {
			$this->data['ip'] = $customer_blacklist_info['ip'];
		} else {
      		$this->data['ip'] = '';
    	}

		$this->display('sale/customer_blacklist_form.tpl');
	}

  	protected function validateForm() {

    	if (!range_length($this->request->post['ip'], 1, 40)) {
      		$this->setMessage('error_ip', $this->_['error_ip']);
			return false;
    	}

	  	return true;
  	}
}
?>